package com.i9he.ping.utils;

import static com.i9he.ping.utils.ConfigUtils.configuration;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class LoggerUtils {

	public static Logger getLoggerByName(String name) {
		boolean consoleAppender = configuration.getBoolean("logger.console", false);
		return getLoggerByName(name, consoleAppender);
	}

	public static Logger getLoggerByName(String name, boolean consoleAppender) {
		Logger logger = Logger.getLogger(name);
		logger.removeAllAppenders();
		logger.setLevel(Level.DEBUG);
		logger.setAdditivity(true);
		DailyRollingFileAppender appender = new DailyRollingFileAppender();
		PatternLayout layout = new PatternLayout();
		String conversionPattern = "[%d] %p %c - %m%n";
		layout.setConversionPattern(conversionPattern);
		appender.setLayout(layout);
		appender.setFile(MyPath.getProjectPath() + "/log/" + name + ".log");
		appender.setEncoding("UTF-8");
		appender.setAppend(true);
		appender.activateOptions();

		logger.addAppender(appender);
		if (consoleAppender) {
			logger.addAppender(new ConsoleAppender(layout));
		}

		return logger;
	}
}
